
// const $math = require('mathjs')
export function px2Rem() {
  /* 
  *PX 转 rem 
  *1rem=14px
  */
  var baseFontSize = 14
  var designWidth = 1920
  var width = window.innerWidth
  var currentFontSize = (width / designWidth) * baseFontSize
  document.getElementsByTagName('html')[0].style.fontSize = currentFontSize + 'px'
}
// export const math = {
//   add() {
//     return comp('add', arguments)
//   },
//   subtract() {
//     return comp('subtract', arguments)
//   },
//   multiply() {
//     return comp('multiply', arguments)
//   },
//   divide() {
//     return comp('divide', arguments)
//   }
// }

// function comp(_func, args) {
//   let t = $math.chain($math.bignumber(args[0]))
//   for (let i = 1; i < args.length; i++) {
//     t = t[_func]($math.bignumber(args[i]))
//   }
//   // 防止超过6位使用科学计数法
//   return parseFloat(t.done())
// }
// 防抖
export function _debounce(fn, delays) {

  var delay = delays || 200;
  var timer;
  return function () {
    var th = this;
    var args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(function () {
      timer = null;
      fn.apply(th, args);
    }, delay);
  };
}
// 节流
export function _throttle(fn, intervals) {
  let last;
  let timer;
  let interval = intervals || 200;
  return function () {
    let th = this;
    let args = arguments;
    let now = +new Date();
    if (last && now - last < interval) {
      clearTimeout(timer);
      timer = setTimeout(function () {
        last = now;
        fn.apply(th, args);
      }, interval);
    } else {
      last = now;
      fn.apply(th, args);
    }
  }
}

export const getCurrentTime = time => {
  let myDate = new Date();
  if (time) {
    myDate = new Date(time);
  }
  let fullYear = myDate.getFullYear();
  let Month = myDate.getMonth() + 1;
  let day = myDate.getDate();
  let hours = myDate.getHours();
  let min = myDate.getMinutes();
  let Seconds = myDate.getSeconds();
  return { fullYear, Month, day, hours, min, Seconds }
}
// 获取时间戳
export const getTime = time => {
  if (time) {
    return new Date(time).getTime();
  } else {
    return new Date().getTime();
  }
}
/**
 * 倒计时统计
 * @param {*} endTime 
 */
export const countDownTime = endTime => {
  endTime = new Date(endTime.replace(/-/g, "/")).getTime();
  let times = new Date().getTime();
  let dateDiff = endTime - times;

  if (dateDiff < 0) return { day: 0, hours: 0, minutes: 0, seconds: 0 };

  let day = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数
  var leave1 = dateDiff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  let hours =
    Math.floor(leave1 / (3600 * 1000)) < 10
      ? "0" + Math.floor(leave1 / (3600 * 1000))
      : Math.floor(leave1 / (3600 * 1000)); //计算出小时数
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  let minutes =
    Math.floor(leave2 / (60 * 1000)) < 10
      ? "0" + Math.floor(leave2 / (60 * 1000))
      : Math.floor(leave2 / (60 * 1000)); //计算相差分钟数
  //计算相差秒数
  var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  let seconds =
    Math.round(leave3 / 1000) < 10
      ? "0" + Math.round(leave3 / 1000)
      : Math.round(leave3 / 1000);
  return { day, hours, minutes, seconds };
}

let STORE = window.localStorage







/**
 * 四舍五入取两位小数
 */
export const numFilter = function (val) {
  let data = parseFloat(Number(val)).toFixed(4);
  return data
}
/**
 * 商品库存除以最小销售数量 取整
 * @param {String} string 
 */
export function goodsqtyFilter(stqy, zxB2bNumLimit) {
  // if(!stqy || !zxB2bNumLimit)return 
  // console.log(stqy, zxB2bNumLimit);
  return parseInt(stqy / zxB2bNumLimit) || 0;
}


/**
 * 
 * @param {*} e 
 */
export function getTop(e) {
  if (e !== undefined && e !== '' && e.offsetTop !== undefined) {
    var offset = e.offsetTop;
    if (e.offsetParent != null) offset += getTop(e.offsetParent);
    return offset;
  }
}
export function getHeight(e) {
  var offset = e.offsetHeight;
  if (e.offsetParent != null) offset += getTop(e.offsetParent);
  return offset;
}
